package com.originskyseed.platform.user.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.text.csv.CsvWriter;
import com.originskyseed.platform.common.utils.CsvUtil;
import com.originskyseed.platform.user.model.*;
import com.originskyseed.platform.user.vo.request.UserCumulativeChargeRequestVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.common.annotation.SysLog;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.core.util.bean.ExportExcelUtil;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.service.IDeptService;
import com.originskyseed.platform.user.service.IPlayerRelateRechargeService;
import com.originskyseed.platform.user.service.IPlayerService;
import com.originskyseed.platform.user.service.ITradeUnionService;
import com.originskyseed.platform.user.service.IUserInfoService;
import com.originskyseed.platform.user.vo.request.PlayerRelateRechargeRequestVO;

/**
 * 玩家充值信息表Controller类（OSI_PLAYER_RELATE_RECHARGE）
 *
 * @author wlj
 */
@Controller
@RequestMapping("/playerRelateRecharge")
@BaseLocaleResource(localeFileName = "i18n_user")
public class PlayerRelateRechargeController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(PlayerRelateRechargeController.class);

	@DubboReference
	private IPlayerRelateRechargeService playerRelateRechargeService;

	@DubboReference
	private ITradeUnionService tradeUnionService;

	@DubboReference
	private IDeptService deptService;

	@DubboReference
	private IUserInfoService userInfoService;

	@DubboReference
	private IPlayerService playerService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/playerRelateList.json")
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<PlayerRelateRecharge>> playerRelateList(HttpServletRequest request) {
		BaseResponseBody<BasePage<PlayerRelateRecharge>> result = null;
		try {
			// 请求对象
			PlayerRelateRechargeRequestVO requestVO = this.getParameterBean(PlayerRelateRechargeRequestVO.class);

			// 用户信息
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			if(userInfo==null)
			{
				result = new BaseResponseBody<BasePage<PlayerRelateRecharge>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.USER_IS_NULL.getMsg()),null, null);
                return result;
			}
			String otherAccount=userInfo.getOtherAccount() == null ? "" : userInfo.getOtherAccount();
			// 分页查询
			BasePage<PlayerRelateRecharge> page = new BasePage<PlayerRelateRecharge>(requestVO.getPageNo(), requestVO.getPageSize());
			// 根据范围查询
			if(userInfo.getSourceSys()!=0) //开放类 查询所有信息
			{
				List<String> otherAccountList = new ArrayList<>();
				if (userInfo.getType() == 6 || userInfo.getType() == 11) {
					if (StringUtils.isNotBlank(otherAccount)) {
						otherAccountList.add(otherAccount);
					}
				} else {
					Dept dept = this.deptService.selectByPrimaryKey(userInfo.getDeptId());
					List<String> deptList = new ArrayList<>();
					if (dept != null && dept.getCode() != null) {
						deptList.add(dept.getId());
						selectSonId(dept.getCode(), deptList);
						otherAccountList = this.userInfoService.selectOtherAccountByDeptIds(deptList, 2);
					}
					if (StringUtils.isNotBlank(otherAccount)) {
						otherAccountList.add(otherAccount);
					}
				}
				if (otherAccountList.size() == 0) {

					return new BaseResponseBody<BasePage<PlayerRelateRecharge>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
				}
				requestVO.setList(otherAccountList);
			}
			page = this.playerRelateRechargeService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<PlayerRelateRecharge>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<PlayerRelateRecharge>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 推广平台_玩家充值记录_分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/playerRelateRechargeList.json")
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<PlayerRelateRecharge>> playerRelateRechargeList(HttpServletRequest request) {
		BaseResponseBody<BasePage<PlayerRelateRecharge>> result = null;
		try {
			// 请求对象
			PlayerRelateRechargeRequestVO requestVO = this.getParameterBean(PlayerRelateRechargeRequestVO.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			BasePage<PlayerRelateRecharge> page = null;
			if (userInfo.getSourceSys() == 2) { // 业务员
				requestVO.setBelongBusinessId(userInfo.getId());
				page = new BasePage<PlayerRelateRecharge>(requestVO.getPageNo(), requestVO.getPageSize());
				page = this.playerRelateRechargeService.selectPlayerRelateRechargeListToSalesman(requestVO.toMap(), page);
			}

			result = new BaseResponseBody<BasePage<PlayerRelateRecharge>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<PlayerRelateRecharge>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 推广平台_玩家充值记录_导出Excel
	 * 
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@RequestMapping("/contentToExcel.json")
	@SysLog(module = "基础模块")
	public void contentToExcel(HttpServletRequest request, HttpServletResponse response) {
		try {
			List<PlayerRelateRecharge> list = null;
			PlayerRelateRechargeRequestVO requestVO = this.getParameterBean(PlayerRelateRechargeRequestVO.class);
			// 用户信息
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			String otherAccount=userInfo.getOtherAccount() == null ? "" : userInfo.getOtherAccount();
			// 根据范围查询
			List<String> otherAccountList = new ArrayList<>();
			if (userInfo.getType() == 6 || userInfo.getType() == 11 ) {
				if(StringUtils.isNotBlank(otherAccount)) {
					otherAccountList.add(otherAccount);
				}
			} else {
				Dept dept = this.deptService.selectByPrimaryKey(userInfo.getDeptId());
				List<String> deptList = new ArrayList<>();
				if (dept != null&&dept.getCode()!=null) {
					deptList.add(dept.getId());
					selectSonId(dept.getCode(), deptList);
					otherAccountList = this.userInfoService.selectOtherAccountByDeptIds(deptList,2);
				}
				if(StringUtils.isNotBlank(otherAccount)) {
					otherAccountList.add(otherAccount);
				}
			}
			if(otherAccountList.size()>0){
				requestVO.setChannelSubIdList(otherAccountList);
				list = this.playerRelateRechargeService.selectContentExcel(requestVO.toMap());
			}

			// excel标题
			String[] title = { "订单号", "用户id", "用户昵称", "业务员账号", "业务员名称", "主渠道", "子渠道", "软件", "订单总金额（元）", "获得物品", "充值时间" };

			// excel文件名
			String fileName = "玩家充值记录信息.xls";
			// sheet名
			String sheetName = "玩家充值记录信息";
			String[][] values = new String[list.size()][];

			if (list != null && list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					values[i] = new String[title.length];
					PlayerRelateRecharge obj = list.get(i);
					values[i][0] = obj.getOrderId() == null ? "" : obj.getOrderId();
					values[i][1] = obj.getPlayerId() == null ? "" : obj.getPlayerId();
					values[i][2] = obj.getNickName() == null ? "" : obj.getNickName();
					values[i][3] = obj.getBelongBusinessAccount() == null ? "" : obj.getBelongBusinessAccount();
					values[i][4] = obj.getBelongBusinessName() == null ? "" : obj.getBelongBusinessName();
					values[i][5] = obj.getChannelId() == null ? "" : obj.getChannelId();
					values[i][6] = obj.getChannelSubId() == null ? "" : obj.getChannelSubId();
					values[i][7] = obj.getSoftware() == null ? "" : obj.getSoftware();
					values[i][8] = obj.getAmount() == null ? "" : obj.getAmount().toString();
					values[i][9] = obj.getSoftware() == null ? "" : obj.getSoftware();
					SimpleDateFormat RechargeTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					values[i][10] = RechargeTime.format(obj.getRechargeTime()) == null ? "" : RechargeTime.format(obj.getRechargeTime());
				}
			}
			outPutExcel(response, title, fileName, sheetName, values);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
		}
	}

	/**
	 * 推广平台_玩家充值记录_导出Excel
	 * 
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@RequestMapping("/exportToExcel.json")
	@SysLog(module = "基础模块", operationDesc = "玩家充值记录信息")
	public void exportToExcel(HttpServletRequest request, HttpServletResponse response) {
		try {
			List<PlayerRelateRecharge> list = null;
			PlayerRelateRechargeRequestVO requestVO = this.getParameterBean(PlayerRelateRechargeRequestVO.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			if (userInfo.getSourceSys() == 2) { // 业务员
				requestVO.setBelongBusinessId(userInfo.getId());
				list = this.playerRelateRechargeService.PlayerRelateRechargeListToSalesman(requestVO.toMap());
			}
			// excel标题
			String[] title = { "订单号", "账号", "所属主渠道名称", "所属子渠道名称", "软件", "订单总金额（元）", "获得物品", "付款方式", "订单状态", "充值时间", "是否结算" };
			// excel文件名
			String fileName = "玩家充值记录信息.xls";
			// sheet名
			String sheetName = "玩家充值记录信息";
			String[][] values = new String[list.size()][];

			if (list != null && list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					values[i] = new String[title.length];
					PlayerRelateRecharge obj = list.get(i);
					values[i][0] = obj.getOrderId() == null ? "" : obj.getOrderId();
					values[i][1] = obj.getAccount() == null ? "" : obj.getAccount();
					values[i][2] = obj.getChannelName() == null ? "" : obj.getChannelName();
					values[i][3] = obj.getChannesubName() == null ? "" : obj.getChannesubName();
					values[i][4] = obj.getSoftware() == null ? "" : obj.getSoftware();
					values[i][5] = obj.getAmount() == null ? "" : obj.getAmount().toString();
					values[i][6] = obj.getGoods() == null ? "" : obj.getGoods();
					if (obj.getPayType() != null) {
						if (obj.getPayType() == 1) {
							values[i][7] = "cms后台操作";
						}
						if (obj.getPayType() == 2) {
							values[i][7] = "微信";
						}
						if (obj.getPayType() == 3) {
							values[i][7] = "支付宝";
						}
					} else {
						values[i][7] = "";
					}
					if (obj.getOrderState() != null) {
						if (obj.getOrderState() == 1) {
							values[i][8] = "未支付";
						}
						if (obj.getOrderState() == 2) {
							values[i][8] = "已付款物品未到账";
						}
						if (obj.getOrderState() == 3) {
							values[i][8] = "已完成";
						}
					} else {
						values[i][8] = "";
					}
					SimpleDateFormat RechargeTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					values[i][9] = RechargeTime.format(obj.getRechargeTime()) == null ? "" : RechargeTime.format(obj.getRechargeTime());
					if (obj.getIsSettlement() != null) {
						if (obj.getIsSettlement() == 1) {
							values[i][10] = "否";
						}
						if (obj.getIsSettlement() == 2) {
							values[i][10] = "是";
						}
					} else {
						values[i][10] = "";
					}
				}
			}
			outPutExcel(response, title, fileName, sheetName, values);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
		}
	}

	/**
	 * 玩家充值信息导出Excel模板
	 * 
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@GetMapping("/excelModel.json")
	@SysLog(module = "直播管理类模块", operationDesc = "玩家充值记录信息excel模板")
	public void excelModel(HttpServletRequest request, HttpServletResponse response) {
		try {
			// excel标题
			String[] title = { "订单号*", "账号*", "所属主渠道名称*", "所属子渠道名称*", "软件*", "订单总金额（元）*", "获得物品*", "付款方式*", "订单状态*", "充值时间*", "是否结算*" };
			// excel文件名
			String fileName = "玩家充值记录信息导出模板.xls";
			// sheet名
			String sheetName = "玩家充值记录信息导出模板";
			String[][] values = new String[0][];
			outPutExcel(response, title, fileName, sheetName, values);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
		}
	}

	/**
	 * 输出Excel
	 *
	 * @param response
	 * @param title
	 * @param fileName
	 * @param sheetName
	 * @param values
	 * @throws IOException
	 */
	private void outPutExcel(HttpServletResponse response, String[] title, String fileName, String sheetName, String[][] values) throws IOException {
		// 创建HSSFWorkbook
		HSSFWorkbook wb = ExportExcelUtil.getHSSFWorkbook(sheetName, title, values, null);
		// 将文件存到指定位置
		this.setResponseHeader(response, fileName);
		OutputStream os = response.getOutputStream();
		wb.write(os);
		os.flush();
		os.close();
	}

	/**
	 * 发送响应流方法
	 *
	 * @param response
	 * @param fileName
	 */
	public void setResponseHeader(HttpServletResponse response, String fileName) {
		try {
			try {
				fileName = new String(fileName.getBytes(), "ISO8859-1");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				logger.error(e.getMessage(), e);
			}
			response.setContentType("application/octet-stream;charset=ISO8859-1");
			response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			response.addHeader("Pargam", "no-cache");
			response.addHeader("Cache-Control", "no-cache");
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.getMessage(), ex);
		}
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/playerRelateRechargeSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<PlayerRelateRecharge> playerRelateRechargeSave(HttpServletRequest request) {
		BaseResponseBody<PlayerRelateRecharge> result = null;
		try {
			// 请求对象
			PlayerRelateRecharge playerRelateRecharge = this.getParameterBean(PlayerRelateRecharge.class);

			if (StringUtils.isNotBlank(playerRelateRecharge.getId())) {
				this.playerRelateRechargeService.updateByPrimaryKey(playerRelateRecharge);
			} else {
				this.playerRelateRechargeService.insertSelective(playerRelateRecharge);
			}
			result = new BaseResponseBody<PlayerRelateRecharge>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), playerRelateRecharge);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<PlayerRelateRecharge>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/playerRelateRechargeDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> playerRelateRechargeDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.playerRelateRechargeService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 推广平台_财务结算_财务结算表 --统计过程还没完善
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/finacialSettlementList.json")
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<PlayerRelateRecharge>> finacialSettlementList(HttpServletRequest request) {
		BaseResponseBody<BasePage<PlayerRelateRecharge>> result = null;
		try {
			// 请求对象
			PlayerRelateRechargeRequestVO requestVO = this.getParameterBean(PlayerRelateRechargeRequestVO.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			BasePage<PlayerRelateRecharge> page = null;
			if (userInfo.getSourceSys() == 2 && (userInfo.getType() == 2 || userInfo.getType() == 3 || userInfo.getType() == 4 || userInfo.getType() == 5)) { // 会长、副会长
				TradeUnion tradeUnion = this.tradeUnionService.selectByUserId(userInfo.getId());
				requestVO.setTradeunionId(tradeUnion.getId());// 公会ID
				requestVO.setOrderState(3);// 已完成
				page = new BasePage<PlayerRelateRecharge>(requestVO.getPageNo(), requestVO.getPageSize());
				page = this.playerRelateRechargeService.finacialSettlementToMonth(requestVO.toMap(), page);
			}
			result = new BaseResponseBody<BasePage<PlayerRelateRecharge>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.FINANCIAL_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<PlayerRelateRecharge>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.FINANCIAL_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 推广平台_财务结算_导出结算函 --统计过程还没完善
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/finacialSettlementLetter.json")
	@RequiredParams(names = {})
	public BaseResponseBody finacialSettlementLetter(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			PlayerRelateRechargeRequestVO requestVO = this.getParameterBean(PlayerRelateRechargeRequestVO.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			SimpleDateFormat RechargeTime = new SimpleDateFormat("yyyy-MM-dd");
			Map<String, Object> resultMap = new HashMap<>();
			if (userInfo.getSourceSys() == 2 && (userInfo.getType() == 2 || userInfo.getType() == 3 || userInfo.getType() == 4 || userInfo.getType() == 5)) { // 会长、副会长
				Map<String, Object> tradeUnionMap = new HashMap<>();
				TradeUnion tradeUnion = this.tradeUnionService.selectByUserId(userInfo.getId());
				if (tradeUnion.getTradeUnionType() == 0) { // 个人
					tradeUnionMap.put("name", tradeUnion.getName());
				} else {
					tradeUnionMap.put("name", tradeUnion.getCompanyName());// 公司
				}
				tradeUnionMap.put("bankName", tradeUnion.getBankName());
				tradeUnionMap.put("branch", tradeUnion.getBranch());
				tradeUnionMap.put("bankAccount", tradeUnion.getBankAccount());

				requestVO.setTradeunionId(tradeUnion.getId());// 公会ID
				requestVO.setOrderState(3);// 已完成
				requestVO.setIsSettlement(1);// 未结算
				List<PlayerRelateRecharge> finacialSettlementLetter = this.playerRelateRechargeService.finacialSettlementLetter(requestVO.toMap());

				resultMap.put("finacialSettlementLetter", finacialSettlementLetter);
				resultMap.put("dateTime", RechargeTime.format(requestVO.getDateMin()) + "-" + RechargeTime.format(requestVO.getDateMax()));
				resultMap.put("account", userInfo.getAccount());// 账号
				resultMap.put("partyB", tradeUnionMap);// 乙方
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.FINANCIAL_LIST_SUCCESS.getMsg()), resultMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.FINANCIAL_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 推广平台_对账表查询 --统计过程还没完善
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/revenueReconCliationList.json")
	@RequiredParams(names = { "pageNo", "pageSize", "channelId" })
	public BaseResponseBody<BasePage<PlayerRelateRecharge>> revenueReconCliationList(HttpServletRequest request) {
		BaseResponseBody<BasePage<PlayerRelateRecharge>> result = null;
		try {
			// 请求对象
			PlayerRelateRechargeRequestVO requestVO = this.getParameterBean(PlayerRelateRechargeRequestVO.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			BasePage<PlayerRelateRecharge> page = null;
			if (StringUtils.isNotBlank(requestVO.getChannelId())) {
				requestVO.setOrderState(3);
				page = new BasePage<PlayerRelateRecharge>(requestVO.getPageNo(), requestVO.getPageSize());
				page = this.playerRelateRechargeService.selectRevenueReconcliationList(requestVO.toMap(), page);
			}
			result = new BaseResponseBody<BasePage<PlayerRelateRecharge>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.REVENUE_RECONCILIATION_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<PlayerRelateRecharge>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.REVENUE_RECONCILIATION_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 财务结算收入对账表导出Excel
	 * 
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@RequestMapping("/RevenueReconciliationToExcel.json")
	@SysLog(module = "基础模块", operationDesc = "财务结算收入对账信息")
	public void RevenueReconciliationToExcel(HttpServletRequest request, HttpServletResponse response) {
		try {
			List<PlayerRelateRecharge> list = null;
			PlayerRelateRechargeRequestVO requestVO = this.getParameterBean(PlayerRelateRechargeRequestVO.class);
			requestVO.setOrderState(3);
			SimpleDateFormat RechargeTime = new SimpleDateFormat("yyyy-MM-dd");
			if (StringUtils.isNotBlank(requestVO.getChannelId())) {
				list = this.playerRelateRechargeService.selectRevenueReconcliationListToExcel(requestVO.toMap());
			}

			// excel标题
			String[] title = { "结算周期", "订单号", "子渠道ID", "子渠道名称", "用户ID", "充值金额（元）", "获得物品", "分成比例(%)", "结算金额（元）", "支付方式" };
			// excel文件名
			String fileName = RechargeTime.format(requestVO.getDateMin()) + "~" + RechargeTime.format(requestVO.getDateMax()) + "财务结算收入对账表信息.xls";
			// sheet名
			String sheetName = "财务结算收入对账表信息";
			String[][] values = new String[list.size()][];

			if (list != null && list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					values[i] = new String[title.length];
					PlayerRelateRecharge obj = list.get(i);
					values[i][0] = RechargeTime.format(requestVO.getDateMin()) + "~" + RechargeTime.format(requestVO.getDateMax());
					values[i][1] = obj.getOrderId() == null ? "" : obj.getOrderId();
					values[i][2] = obj.getChannelSubId() == null ? "" : obj.getChannelSubId();
					values[i][3] = obj.getChannesubName() == null ? "" : obj.getChannesubName();
					values[i][4] = obj.getPlayerId() == null ? "" : obj.getPlayerId();
					values[i][5] = obj.getAmount() == null ? "" : obj.getAmount().toString();
					values[i][6] = obj.getGoods() == null ? "" : obj.getGoods();
					values[i][7] = obj.getProportion() == null ? "" : obj.getProportion().toString();
					values[i][8] = obj.getSettleAccount() == null ? "" : obj.getSettleAccount().toString();
					if (obj.getPayType() != null) {
						if (obj.getPayType() == 1) {
							values[i][9] = "cms后台操作";
						}
						if (obj.getPayType() == 2) {
							values[i][9] = "微信";
						}
						if (obj.getPayType() == 3) {
							values[i][9] = "支付宝";
						}
					} else {
						values[i][9] = "";
					}

				}
			}
			outPutExcel(response, title, fileName, sheetName, values);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
		}
	}

	/**
	 * 财务结算收入对账表导出Excel模板
	 * 
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@GetMapping("/RevenueReconciliationToExcelModel.json")
	@SysLog(module = "直播管理类模块", operationDesc = "财务结算收入对账表导出Excel模板")
	public void RevenueReconciliationToExcelModel(HttpServletRequest request, HttpServletResponse response) {
		try {
			// excel标题
			String[] title = { "结算周期*", "订单号*", "子渠道ID*", "子渠道名称*", "用户ID*", "充值金额（元）*", "获得物品*", "分成比例*", "结算金额（元）*", "支付方式*" };
			// excel文件名
			String fileName = "财务结算收入对账表信息导出模板.xls";
			// sheet名
			String sheetName = "财务结算收入对账表信息导出模板";
			String[][] values = new String[0][];
			outPutExcel(response, title, fileName, sheetName, values);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
		}
	}

	/**
	 * 查询充值总额
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectTotal.json", method = RequestMethod.POST)
	public BaseResponseBody selectTotal(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			PlayerRelateRechargeRequestVO requestVO = this.getParameterBean(PlayerRelateRechargeRequestVO.class);

			BigDecimal total = this.playerRelateRechargeService.selectTotalAmountByMap(requestVO.toMap());
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), total);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 支付明细查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectPaymentDetails.json")
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody selectPaymentDetails(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 当前登录用户
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			if(userInfo==null)
			{
				result = new BaseResponseBody<BasePage<PlayerRelateRecharge>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.USER_IS_NULL.getMsg()),null, null);
                return result;
			}
			String otherAccount=userInfo.getOtherAccount() == null ? "" : userInfo.getOtherAccount();
			// 请求对象
			PlayerRelateRechargeRequestVO requestVO = this.getParameterBean(PlayerRelateRechargeRequestVO.class);
			// 根据范围查询
			List<String> otherAccountList = new ArrayList<>();
			if(userInfo.getSourceSys()!=0) //开放类 查询所有信息
			{
				if (userInfo.getType() == 6 || userInfo.getType() == 11) {
					if (StringUtils.isNotBlank(otherAccount)) {
						otherAccountList.add(otherAccount);
					}
				} else {
					Dept dept = this.deptService.selectByPrimaryKey(userInfo.getDeptId());
					List<String> deptList = new ArrayList<>();
					if (dept != null && dept.getCode() != null) {
						deptList.add(dept.getId());
						selectSonId(dept.getCode(), deptList);
						otherAccountList = this.userInfoService.selectOtherAccountByDeptIds(deptList, 2);
					}
					if (StringUtils.isNotBlank(otherAccount)) {
						otherAccountList.add(otherAccount);
					}
				}
				if (otherAccountList.size() == 0) {
					return new BaseResponseBody<BasePage<PlayerRelateRecharge>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), new BasePage<>(requestVO.getPageNo(), requestVO.getPageSize()));
				}
				requestVO.setChannelSubIdList(otherAccountList);
			}
			// 分页查询
			BasePage<PlayerRelateRecharge> page = new BasePage<PlayerRelateRecharge>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.playerRelateRechargeService.selectPaymentDetailsByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<PlayerRelateRecharge>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<PlayerRelateRecharge>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}


	/**
	 * 导出信息Excel
	 *
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/export.json")
	public void logExport(HttpServletRequest request, HttpServletResponse response) {
		try {
			// 当前登录用户
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			String otherAccount=userInfo.getOtherAccount() == null ? "" : userInfo.getOtherAccount();
			// 请求对象
			PlayerRelateRechargeRequestVO requestVO = this.getParameterBean(PlayerRelateRechargeRequestVO.class);
			// 根据范围查询
			List<String> otherAccountList = new ArrayList<>();
			if(userInfo.getSourceSys()!=0) //开放类 查询所有信息
			{
				if (userInfo.getType() == 6 || userInfo.getType() == 11) {
					if (StringUtils.isNotBlank(otherAccount)) {
						otherAccountList.add(otherAccount);
					}
				} else {
					Dept dept = this.deptService.selectByPrimaryKey(userInfo.getDeptId());
					List<String> deptList = new ArrayList<>();
					if (dept != null && dept.getCode() != null) {
						deptList.add(dept.getId());
						selectSonId(dept.getCode(), deptList);
						otherAccountList = this.userInfoService.selectOtherAccountByDeptIds(deptList, 2);
					}
					if (StringUtils.isNotBlank(otherAccount)) {
						otherAccountList.add(otherAccount);
					}
				}
				if (otherAccountList.size() == 0) {
					return;
				}
				requestVO.setChannelSubIdList(otherAccountList);
			}

			List<PlayerRelateRecharge> list = this.playerRelateRechargeService.selectPaymentDetailsByMap(requestVO.toMap());
			Map<String, String> header = new LinkedHashMap<>();
			header.put("playerId", "玩家id");
			header.put("nickName", "玩家昵称");
			header.put("orderId", "充值订单");
			header.put("rechargeTime", "支付时间");
			header.put("amount", "充值金额");
			header.put("totalAmount", "累计金额");
			header.put("goods", "获得物品");
			CsvUtil.export(request, response, "支付明细", header, list);

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}


	/**
	 * 查询组织下的部门
	 *
	 * @param code
	 * @param list
	 * @throws Exception
	 */
	private void selectSonId(String code, List<String> list) throws Exception {
		List<Dept> deptList = this.deptService.selectByParentCode(code);
		if (deptList != null) {
			for (Dept dept1 : deptList) {
				list.add(dept1.getId());
				selectSonId(dept1.getCode(), list);
			}
		}
	}

}
